<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

use addons\cms\model\SearchLog;
/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * Archives模型对象
     */
    protected $model = null;
    protected $noNeedRight = [];
    protected $isSuperAdmin = false;
    protected $searchFields = 'id,title';

    public function index1()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $config = get_addon_config('cms');
        if ($this->request->isPost()) {
            $date = $this->request->post('date', '');
            $type = $this->request->post('type', '');
            if ($type == 'sale') {
                list($orderSaleCategory, $orderSaleAmount, $orderSaleNums) = $this->getSaleStatisticsData($date);
                $statistics = ['orderSaleCategory' => $orderSaleCategory, 'orderSaleAmount' => $orderSaleAmount, 'orderSaleNums' => $orderSaleNums];
            } elseif ($type == 'percent') {
                list($orderPercentCategory, $orderPercentAmount, $orderPercentNums) = $this->getPercentStatisticsData($date);
                $statistics = ['orderPercentCategory' => $orderPercentCategory, 'orderPercentAmount' => $orderPercentAmount, 'orderPercentNums' => $orderPercentNums];
            } elseif ($type == 'order') {
                list($category, $data) = $this->getOrderStatisticsData($date);
                $statistics = ['category' => $category, 'data' => $data];
            } elseif ($type == 'archives') {
                list($category, $data) = $this->getArchivesStatisticsData($date);
                $statistics = ['category' => $category, 'data' => $data];
            }
            $this->success('', '', $statistics);
        }

        //管理员发文统计图表
        list($category, $data) = $this->getArchivesStatisticsData('');
        $this->assignconfig('adminArchivesListCategory', $category);
        $this->assignconfig('adminArchivesListData', $data);

        //今日订单和会员
        $totalOrderAmount = round(\app\admin\model\cms\Order::where('status', 'paid')->sum('payamount'), 2);
        $yesterdayOrderAmount = round(\app\admin\model\cms\Order::where('status', 'paid')->whereTime('paytime', 'yesterday')->sum('payamount'), 2);
        $todayOrderAmount = round(\app\admin\model\cms\Order::where('status', 'paid')->whereTime('paytime', 'today')->sum('payamount'), 2);
        $todayOrderRatio = $yesterdayOrderAmount > 0 ? ceil((($todayOrderAmount - $yesterdayOrderAmount) / $yesterdayOrderAmount) * 100) : ($todayOrderAmount > 0 ? 100 : 0);

        $totalUser = User::count();
        $yesterdayUser = User::whereTime('jointime', 'yesterday')->count();
        $todayUser = User::whereTime('jointime', 'today')->count();
        $todayUserRatio = $yesterdayUser > 0 ? ceil((($todayUser - $yesterdayUser) / $yesterdayUser) * 100) : ($todayUser > 0 ? 100 : 0);

        //文档和评论统计
        $totalArchives = \app\admin\model\cms\Archives::count();
        $unsettleArchives = \app\admin\model\cms\Archives::where('status', 'hidden')->count();
        $totalComment = \app\admin\model\cms\Comment::count();
        $unsettleComment = \app\admin\model\cms\Comment::where('status', 'hidden')->count();
        $diyformList = \app\admin\model\cms\Diyform::all();
        foreach ($diyformList as $index => $item) {
            $item->nums = \think\Db::name($item['table'])->count();
        }

        //订单数和订单额统计
        list($orderSaleCategory, $orderSaleAmount, $orderSaleNums) = $this->getSaleStatisticsData();
        $this->assignconfig('orderSaleCategory', $orderSaleCategory);
        $this->assignconfig('orderSaleAmount', $orderSaleAmount);
        $this->assignconfig('orderSaleNums', $orderSaleNums);

        //订单占比统计
        list($orderPercentCategory, $orderPercentAmount, $orderPercentNums) = $this->getPercentStatisticsData();
        $this->assignconfig('orderPercentCategory', $orderPercentCategory);
        $this->assignconfig('orderPercentAmount', $orderPercentAmount);
        $this->assignconfig('orderPercentNums', $orderPercentNums);

        //热门标签
        $tagList = \app\admin\model\cms\Tag::order('nums', 'desc')->limit(10)->select();
        $tagsTotal = 0;
        foreach ($tagList as $index => $item) {
            $tagsTotal += $item['nums'];
        }
        foreach ($tagList as $index => $item) {
            $item['percent'] = $tagsTotal > 0 ? round($item['nums'] / $tagsTotal * 100, 2) : 0;
        }

        //热门搜索列表
        $hotSearchList = SearchLog::order('nums', 'desc')->cache(3600, null, 'cms')->limit(10)->select();
        $hotTagList = \addons\cms\model\Tag::order('nums', 'desc')->cache(3600, null, 'cms')->limit(10)->select();
        $hotArchivesList = \addons\cms\model\Archives::order('views', 'desc')->cache(3600, null, 'cms')->limit(10)->select();

        //付费排行榜
        $todayPaidTotal = \app\admin\model\cms\Order::whereTime('paytime', 'today')->sum("payamount");
        $todayPaidList = \app\admin\model\cms\Order::with(['archives'])->whereTime('paytime', 'today')->group('archives_id')->field("COUNT(*) as nums,SUM(payamount) as amount,archives_id")->order("amount", "desc")->limit(10)->select();
        foreach ($todayPaidList as $index => $item) {
            $item->percent = $todayPaidTotal > 0 ? round(($item['amount'] / $todayPaidTotal) * 100, 2) : 0;
        }

        $weekPaidTotal = \app\admin\model\cms\Order::whereTime('paytime', 'week')->sum("payamount");
        $weekPaidList = \app\admin\model\cms\Order::with(['archives'])->whereTime('paytime', 'week')->group('archives_id')->field("COUNT(*) as nums,SUM(payamount) as amount,archives_id")->order("amount", "desc")->limit(10)->select();
        foreach ($weekPaidList as $index => $item) {
            $item->percent = $weekPaidTotal > 0 ? round(($item['amount'] / $weekPaidTotal) * 100, 2) : 0;
        }

        $monthPaidTotal = \app\admin\model\cms\Order::whereTime('paytime', 'month')->sum("payamount");
        $monthPaidList = \app\admin\model\cms\Order::with(['archives'])->whereTime('paytime', 'month')->group('archives_id')->field("COUNT(*) as nums,SUM(payamount) as amount,archives_id")->order("amount", "desc")->limit(10)->select();
        foreach ($monthPaidList as $index => $item) {
            $item->percent = $monthPaidTotal > 0 ? round(($item['amount'] / $monthPaidTotal) * 100, 2) : 0;
        }

        //投稿排行榜
        $todayContributeTotal = \app\admin\model\cms\Archives::whereTime('createtime', 'today')->count();
        $todayContributeList = \app\admin\model\cms\Archives::with(['user'])->where('user_id', '>', 0)->whereTime('createtime', 'today')->group('user_id')->field("COUNT(*) as nums,user_id")->order("nums", "desc")->limit(10)->select();
        foreach ($todayContributeList as $index => $item) {
            $item->percent = $todayContributeTotal > 0 ? round(($item['nums'] / $todayContributeTotal) * 100, 2) : 0;
        }

        $weekContributeTotal = \app\admin\model\cms\Archives::whereTime('createtime', 'week')->count();
        $weekContributeList = \app\admin\model\cms\Archives::with(['user'])->where('user_id', '>', 0)->whereTime('createtime', 'week')->group('user_id')->field("COUNT(*) as nums,user_id")->order("nums", "desc")->limit(10)->select();
        foreach ($weekContributeList as $index => $item) {
            $item->percent = $weekContributeTotal > 0 ? round(($item['nums'] / $weekContributeTotal) * 100, 2) : 0;
        }

        $monthContributeTotal = \app\admin\model\cms\Archives::whereTime('createtime', 'month')->count();
        $monthContributeList = \app\admin\model\cms\Archives::with(['user'])->where('user_id', '>', 0)->whereTime('createtime', 'month')->group('user_id')->field("COUNT(*) as nums,user_id")->order("nums", "desc")->limit(10)->select();
        foreach ($monthContributeList as $index => $item) {
            $item->percent = $monthContributeTotal > 0 ? round(($item['nums'] / $monthContributeTotal) * 100, 2) : 0;
        }

        $this->view->assign("totalOrderAmount", $totalOrderAmount);
        $this->view->assign("yesterdayOrderAmount", $yesterdayOrderAmount);
        $this->view->assign("todayOrderAmount", $todayOrderAmount);
        $this->view->assign("todayOrderRatio", $todayOrderRatio);

        $this->view->assign("totalUser", $totalUser);
        $this->view->assign("yesterdayUser", $yesterdayUser);
        $this->view->assign("todayUser", $todayUser);
        $this->view->assign("todayUserRatio", $todayUserRatio);

        $this->view->assign("totalArchives", $totalArchives);
        $this->view->assign("unsettleArchives", $unsettleArchives);
        $this->view->assign("totalComment", $totalComment);
        $this->view->assign("unsettleComment", $unsettleComment);

        $this->view->assign("tagsList", $tagList);
        $this->view->assign("hotTagList", $hotTagList);
        $this->view->assign("hotArchivesList", $hotArchivesList);
        $this->view->assign("hotSearchList", $hotSearchList);

        $this->view->assign("todayPaidList", $todayPaidList);
        $this->view->assign("weekPaidList", $weekPaidList);
        $this->view->assign("monthPaidList", $monthPaidList);

        $this->view->assign("todayContributeList", $todayContributeList);
        $this->view->assign("weekContributeList", $weekContributeList);
        $this->view->assign("monthContributeList", $monthContributeList);

        $this->view->assign("modelList", \app\admin\model\cms\Modelx::order('id asc')->select());

        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }

        if ($this->request->isPost()) {
            $date = $this->request->post('date', '');
            $type = $this->request->post('type', '');
            $model_id = $this->request->post('model_id', '');
            if ($type == 'sale') {
                list($orderSaleCategory, $orderSaleAmount, $orderSaleNums) = $this->getSaleStatisticsData($date);
                $statistics = ['orderSaleCategory' => $orderSaleCategory, 'orderSaleAmount' => $orderSaleAmount, 'orderSaleNums' => $orderSaleNums];
            } elseif ($type == 'percent') {
                list($orderPercentCategory, $orderPercentAmount, $orderPercentNums) = $this->getPercentStatisticsData($date);
                $statistics = ['orderPercentCategory' => $orderPercentCategory, 'orderPercentAmount' => $orderPercentAmount, 'orderPercentNums' => $orderPercentNums];
            } elseif ($type == 'order') {
                list($category, $data) = $this->getOrderStatisticsData($date ,$model_id);
                $statistics = ['category' => $category, 'data' => $data];
            }
            $this->success('', '', $statistics);
        }

        // 维修工单营业总额、安装工单营业总额、商城订单营业总额、总营业额
        $repairAmount = \app\admin\model\Order::where(['type'=>'repair', 'paid'=>'1', 'status'=>['in',[0,1,3]]])->sum('payamount');
        $installAmount = \app\admin\model\Order::where(['type'=>'install', 'paid'=>'1', 'status'=>['in',[0,1,3]]])->sum('payamount');
        $shopAmount = \app\admin\model\ShopOrder::where(['paid'=>'1', 'status'=>['in',[0,1,3]]])->sum('payamount');
        $totalAmount = $repairAmount + $installAmount + $shopAmount;
        $this->view->assign("totalAmount", $totalAmount);
        $this->view->assign("repairAmount", $repairAmount);
        $this->view->assign("installAmount", $installAmount);
        $this->view->assign("shopAmount", $shopAmount);

        // 维修工单昨日营业总额、安装工单昨日营业总额、商城订单昨日营业总额、昨日总营业额
        $repairYesterdayAmount = \app\admin\model\Order::where(['type'=>'repair', 'paid'=>'1', 'status'=>['in',[0,1, 3]]])->whereTime('createtime', 'yesterday')->sum('payamount');
        $installYesterdayAmount = \app\admin\model\Order::where(['type'=>'install', 'paid'=>'1', 'status'=>['in',[0,1,3]]])->whereTime('createtime', 'yesterday')->sum('payamount');
        $shopYesterdayAmount = \app\admin\model\ShopOrder::where(['paid'=>'1', 'status'=>['in',[0,1,3]]])->whereTime('createtime', 'yesterday')->sum('payamount');
        $totalYesterdayAmount = $repairYesterdayAmount + $installYesterdayAmount + $shopYesterdayAmount;
        $this->view->assign("totalYesterdayAmount", $totalYesterdayAmount);
        $this->view->assign("repairYesterdayAmount", $repairYesterdayAmount);
        $this->view->assign("installYesterdayAmount", $installYesterdayAmount);
        $this->view->assign("shopYesterdayAmount", $shopYesterdayAmount);

        // 维修工单今日营业总额、安装工单今日营业总额、商城订单今日营业总额、今日总营业额
        $repairTodayAmount = \app\admin\model\Order::where(['type'=>'repair', 'paid'=>'1', 'status'=>['in',[0,1, 3]]])
            ->whereTime('createtime', 'yesterday')->sum('payamount');
        $installTodayAmount = \app\admin\model\Order::where(['type'=>'install', 'paid'=>'1', 'status'=>['in',[0,1,3]]])->whereTime('createtime', 'yesterday')->sum('payamount');
        $shopTodayAmount = \app\admin\model\ShopOrder::where(['paid'=>'1', 'status'=>['in',[0,1,3]]])->whereTime('createtime', 'yesterday')->sum('payamount');
        $totalTodayAmount = $repairTodayAmount + $installTodayAmount + $shopTodayAmount;
        $this->view->assign("totalTodayAmount", $totalTodayAmount);
        $this->view->assign("repairTodayAmount", $repairTodayAmount);
        $this->view->assign("installTodayAmount", $installTodayAmount);
        $this->view->assign("shopTodayAmount", $shopTodayAmount);

        // 总营业额变化率、维修工单营业总额变化率、安装工单营业总额变化率、商城订单营业总额变化率
        $totalRatio = $totalYesterdayAmount > 0 ? ceil((($totalTodayAmount - $totalYesterdayAmount) / $totalYesterdayAmount) * 100) : ($totalTodayAmount > 0 ? 100 : 0);
        $repairRatio = $repairYesterdayAmount > 0 ? ceil((($repairTodayAmount - $repairYesterdayAmount) / $repairYesterdayAmount) * 100) : ($repairTodayAmount > 0 ? 100 : 0);
        $installRatio = $installYesterdayAmount > 0 ? ceil((($installTodayAmount - $installYesterdayAmount) / $installYesterdayAmount) * 100) : ($installTodayAmount > 0 ? 100 : 0);
        $shopRatio = $shopYesterdayAmount > 0 ? ceil((($shopTodayAmount - $shopYesterdayAmount) / $shopYesterdayAmount) * 100) : ($shopTodayAmount > 0 ? 100 : 0);
        $this->view->assign("totalRatio", $totalRatio);
        $this->view->assign("repairRatio", $repairRatio);
        $this->view->assign("installRatio", $installRatio);
        $this->view->assign("shopRatio", $shopRatio);

        // 用户访问量、订单量、商城订单量、车两信息量
        $userVisitCount = \app\admin\model\User::where(['group_id'=>1,'logintime'=>['<>','null'],'status'=>'normal'])->count();
        $workOrderCount = \app\admin\model\Order::count();
        $shopOrderCount = \app\admin\model\ShopOrder::count();
        $carOrderCount = \app\admin\model\CarOrder::count();
        $this->view->assign("userVisitCount", $userVisitCount);
        $this->view->assign("workOrderCount", $workOrderCount);
        $this->view->assign("shopOrderCount", $shopOrderCount);
        $this->view->assign("carOrderCount", $carOrderCount);

        // 昨日用户访问量、昨日订单量、昨日商城订单量、昨日车两信息量
        $userVisitYesterdayCount = \app\admin\model\User::where(['group_id'=>1,'status'=>'normal'])->whereTime('logintime', 'yesterday')->count();
        $workOrderYesterdayCount = \app\admin\model\Order::whereTime('createtime', 'yesterday')->count();
        $shopOrderYesterdayCount = \app\admin\model\ShopOrder::whereTime('createtime', 'yesterday')->count();
        $carOrderYesterdayCount = \app\admin\model\CarOrder::whereTime('createtime', 'yesterday')->count();
        $this->view->assign("userVisitYesterdayCount", $userVisitYesterdayCount);
        $this->view->assign("workOrderYesterdayCount", $workOrderYesterdayCount);
        $this->view->assign("shopOrderYesterdayCount", $shopOrderYesterdayCount);
        $this->view->assign("carOrderYesterdayCount", $carOrderYesterdayCount);

        // 今日用户访问量、今日订单量、今日商城订单量、今日车两信息量
        $userVisitTodayCount = \app\admin\model\User::where(['group_id'=>1,'status'=>'normal'])->whereTime('logintime', 'today')->count();
        $workOrderTodayCount = \app\admin\model\Order::whereTime('createtime', 'today')->count();
        $shopOrderTodayCount = \app\admin\model\ShopOrder::whereTime('createtime', 'today')->count();
        $carOrderTodayCount = \app\admin\model\CarOrder::whereTime('createtime', 'today')->count();
        $this->view->assign("userVisitTodayCount", $userVisitTodayCount);
        $this->view->assign("workOrderTodayCount", $workOrderTodayCount);
        $this->view->assign("shopOrderTodayCount", $shopOrderTodayCount);
        $this->view->assign("carOrderTodayCount", $carOrderTodayCount);

        // 用户访问量变化率、订单量变化率、商城订单量变化率、车两信息量变化率
        $userVisitRatio = $userVisitYesterdayCount > 0 ? ceil((($userVisitTodayCount - $userVisitYesterdayCount) / $userVisitYesterdayCount) * 100) : ($userVisitTodayCount > 0 ? 100 : 0);
        $workOrderRatio = $workOrderYesterdayCount > 0 ? ceil((($workOrderTodayCount - $workOrderYesterdayCount) / $workOrderYesterdayCount) * 100) : ($workOrderTodayCount > 0 ? 100 : 0);
        $shopOrderRatio = $shopOrderYesterdayCount > 0 ? ceil((($shopOrderTodayCount - $shopOrderYesterdayCount) / $shopOrderYesterdayCount) * 100) : ($shopOrderTodayCount > 0 ? 100 : 0);
        $carOrderRatio = $carOrderYesterdayCount > 0 ? ceil((($carOrderTodayCount - $carOrderYesterdayCount) / $carOrderYesterdayCount) * 100) : ($carOrderTodayCount > 0 ? 100 : 0);
        $this->view->assign("userVisitRatio", $userVisitRatio);
        $this->view->assign("workOrderRatio", $workOrderRatio);
        $this->view->assign("shopOrderRatio", $shopOrderRatio);
        $this->view->assign("carOrderRatio", $carOrderRatio);

        // 用户总数、师傅总数、冻结师傅总数、车商认证总数
        $userCount = \app\admin\model\User::where(['group_id'=>1,'status'=>'normal'])->count();
        $masterCount = \app\admin\model\User::where(['group_id'=>2,'status'=>'normal'])->count();
        $frozenMasterCount = \app\admin\model\User::where(['group_id'=>2,'status'=>'hidden'])->count();
        $carDealerCount = \app\admin\model\CarDealerCertification::where(['status'=>1])->count();
        $this->view->assign("userCount", $userCount);
        $this->view->assign("masterCount", $masterCount);
        $this->view->assign("frozenMasterCount", $frozenMasterCount);
        $this->view->assign("carDealerCount", $carDealerCount);

        // 昨日新增用户数、昨日新增师傅数、昨日冻结师傅数、昨日车商认证数
        $userYesterdayCount = \app\admin\model\User::where(['group_id'=>1,'status'=>'normal'])->whereTime('createtime', 'yesterday')->count();
        $masterYesterdayCount = \app\admin\model\User::where(['group_id'=>2,'status'=>'normal'])->whereTime('createtime', 'yesterday')->count();
        $frozenMasterYesterdayCount = \app\admin\model\User::where(['group_id'=>2,'status'=>'hidden'])->whereTime('createtime', 'yesterday')->count();
        $carDealerYesterdayCount = \app\admin\model\CarDealerCertification::where(['status'=>1])->whereTime('createtime', 'yesterday')->count();
        $this->view->assign("userYesterdayCount", $userYesterdayCount);
        $this->view->assign("masterYesterdayCount", $masterYesterdayCount);
        $this->view->assign("frozenMasterYesterdayCount", $frozenMasterYesterdayCount);
        $this->view->assign("carDealerYesterdayCount", $carDealerYesterdayCount);

        // 今日新增用户数、今日新增师傅数、今日冻结师傅数、今日车商认证数
        $userTodayCount = \app\admin\model\User::where(['group_id'=>1,'status'=>'normal'])->whereTime('createtime', 'today')->count();
        $masterTodayCount = \app\admin\model\User::where(['group_id'=>2,'status'=>'normal'])->whereTime('createtime', 'today')->count();
        $frozenMasterTodayCount = \app\admin\model\User::where(['group_id'=>2,'status'=>'hidden'])->whereTime('createtime', 'today')->count();
        $carDealerTodayCount = \app\admin\model\CarDealerCertification::where(['status'=>1])->whereTime('createtime', 'today')->count();
        $this->view->assign("userTodayCount", $userTodayCount);
        $this->view->assign("masterTodayCount", $masterTodayCount);
        $this->view->assign("frozenMasterTodayCount", $frozenMasterTodayCount);
        $this->view->assign("carDealerTodayCount", $carDealerTodayCount);

        // 用户总数变化率、师傅总数变化率、冻结师傅总数变化率、车商认证总数变化率
        $userRatio = $userYesterdayCount > 0 ? ceil((($userTodayCount - $userYesterdayCount) / $userYesterdayCount) * 100) : ($userTodayCount > 0 ? 100 : 0);
        $masterRatio = $masterYesterdayCount > 0 ? ceil((($masterTodayCount - $masterYesterdayCount) / $masterYesterdayCount) * 100) : ($masterTodayCount > 0 ? 100 : 0);
        $frozenMasterRatio = $frozenMasterYesterdayCount > 0 ? ceil((($frozenMasterTodayCount - $frozenMasterYesterdayCount) / $frozenMasterYesterdayCount) * 100) : ($frozenMasterTodayCount > 0 ? 100 : 0);
        $carDealerRatio = $carDealerYesterdayCount > 0 ? ceil((($carDealerTodayCount - $carDealerYesterdayCount) / $carDealerYesterdayCount) * 100) : ($carDealerTodayCount > 0 ? 100 : 0);
        $this->view->assign("userRatio", $userRatio);
        $this->view->assign("masterRatio", $masterRatio);
        $this->view->assign("frozenMasterRatio", $frozenMasterRatio);
        $this->view->assign("carDealerRatio", $carDealerRatio);

        // 昨日提现金额、今日提现金额、提现总金额、提现总数变化率
        $withdrawalYesterdayAmount = \app\admin\model\UserWithdrawalApply::where(['status'=>1])->whereTime('createtime', 'yesterday')->sum('money');
        $withdrawalYesterdayAmount = 0-$withdrawalYesterdayAmount;
        $withdrawalTodayAmount = \app\admin\model\UserWithdrawalApply::where(['status'=>1])->whereTime('createtime', 'today')->sum('money');
        $withdrawalTodayAmount = 0-$withdrawalTodayAmount;
        $withdrawalAmount = \app\admin\model\UserWithdrawalApply::where(['status'=>1])->sum('money');
        $withdrawalAmount = 0-$withdrawalAmount;
        $withdrawalRatio = $withdrawalYesterdayAmount > 0 ? ceil((($withdrawalTodayAmount - $withdrawalYesterdayAmount) / $withdrawalYesterdayAmount) * 100) : ($withdrawalTodayAmount > 0 ? 100 : 0);
        $this->view->assign("withdrawalYesterdayAmount", $withdrawalYesterdayAmount);
        $this->view->assign("withdrawalTodayAmount", $withdrawalTodayAmount);
        $this->view->assign("withdrawalAmount", $withdrawalAmount);
        $this->view->assign("withdrawalRatio", $withdrawalRatio);


        //订单数和订单额统计
        list($orderSaleCategory, $orderSaleAmount, $orderSaleNums) = $this->getSaleStatisticsData();
        $this->assignconfig('orderSaleCategory', $orderSaleCategory);
        $this->assignconfig('orderSaleAmount', $orderSaleAmount);
        $this->assignconfig('orderSaleNums', $orderSaleNums);


        return $this->view->fetch();
    }

    /**
     * 获取订单销量销售额统计数据
     * @param string $date
     * @return array
     */
    protected function getSaleStatisticsData($date = '')
    {
        $starttime = \fast\Date::unixtime();
        $endtime = \fast\Date::unixtime('day', 0, 'end');

        $format = '%H:00';

        $orderList = \app\admin\model\Order::where('paytime', 'between time', [$starttime, $endtime])
            ->field('paytime, status, COUNT(*) AS nums, SUM(payamount) AS amount, MIN(paytime) AS min_paytime, MAX(paytime) AS max_paytime, 
            DATE_FORMAT(FROM_UNIXTIME(paytime), "' . $format . '") AS paydate')
            ->group('paydate')
            ->select();
        $column = [];
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("H:00", $time);
            $time += 3600;
        }
        $orderSaleNums = $orderSaleAmount = array_fill_keys($column, 0);
        foreach ($orderList as $k => $v) {
            $orderSaleNums[$v['paydate']] = $v['nums'];
            $orderSaleAmount[$v['paydate']] = round($v['amount'], 2);
        }
        $orderSaleCategory = array_keys($orderSaleAmount);
        $orderSaleAmount = array_values($orderSaleAmount);
        $orderSaleNums = array_values($orderSaleNums);
        return [$orderSaleCategory, $orderSaleAmount, $orderSaleNums];
    }

    /**
     * 获取订单占比统计数据
     * @param string $date
     * @return array
     */
    protected function getPercentStatisticsData($date = '')
    {
        $starttime = \fast\Date::unixtime();
        $endtime = \fast\Date::unixtime('day', 0, 'end');
        $modelList = [];
        $orderPercentCategory = $orderPercentAmount = $orderPercentNums = [];
        $list = \app\admin\model\cms\Order::with('archives')
            ->where('order.createtime', 'between time', [$starttime, $endtime])
            ->where('order.status', 'paid')
            ->field("archives.model_id,SUM(payamount) as amount,COUNT(*) as nums")
            ->group('archives.model_id')
            ->select();
        foreach ($list as $index => $item) {
            $modelList[$item['archives']['model']['name']] = $item['amount'];
            $name = $item['archives']['model']['name'];
            $name = $name ? $name : "其它";
            $orderPercentCategory[] = $name;
            $orderPercentAmount[] = ['value' => round($item['amount'], 2), 'name' => $name];
            $orderPercentNums[] = ['value' => $item['nums'], 'name' => $name];
        }
        if (!$orderPercentCategory) {
            $orderPercentCategory = [""];
            $orderPercentNums = [['value' => 0, 'name' => '订单数']];
            $orderPercentAmount = [['value' => 0, 'name' => '订单额']];
        }
        return [$orderPercentCategory, $orderPercentAmount, $orderPercentNums];
    }

    /**
     * 获取订单统计数据
     * @param string $date
     * @return array
     */
    protected function getOrderStatisticsData($date = '',$model_id = '')
    {
        if ($date) {
            list($start, $end) = explode(' - ', $date);

            $starttime = strtotime($start);
            $endtime = strtotime($end);
        } else {
            $starttime = \fast\Date::unixtime('day', 0, 'begin');
            $endtime = \fast\Date::unixtime('day', 0, 'end');
        }
        $totalseconds = $endtime - $starttime;

        $format = '%Y-%m-%d';
        if ($totalseconds > 86400 * 30 * 2) {
            $format = '%Y-%m';
        } else {
            if ($totalseconds > 86400) {
                $format = '%Y-%m-%d';
            } else {
                $format = '%H:00';
            }
        }

        $model_id = $this->request->post("model_id");
        if($model_id == 1){
            $orderList = \app\admin\model\Order::where(['type'=>'install'])->where('paytime', 'between time', [$starttime,
                $endtime])
                ->field('paytime, status, SUM(payamount) AS amount, MIN(paytime) AS min_paytime, MAX(paytime) AS max_paytime, 
            DATE_FORMAT(FROM_UNIXTIME(createtime), "' . $format . '") AS pay_date')
                ->group('pay_date')
                ->select();
        }
        if($model_id == 2){
            $orderList = \app\admin\model\Order::where(['type'=>'repair'])->where('paytime', 'between time', [$starttime, $endtime])
                ->field('paytime, status, SUM(payamount) AS amount, MIN(paytime) AS min_paytime, MAX(paytime) AS max_paytime, 
            DATE_FORMAT(FROM_UNIXTIME(createtime), "' . $format . '") AS pay_date')
                ->group('pay_date')
                ->select();
        }
        if($model_id == 3){
            $orderList = \app\admin\model\ShopOrder::where('paytime', 'between time', [$starttime, $endtime])
                ->field('paytime, status, SUM(payamount) AS amount, MIN(paytime) AS min_paytime, MAX(paytime) AS max_paytime, 
            DATE_FORMAT(FROM_UNIXTIME(createtime), "' . $format . '") AS pay_date')
                ->group('pay_date')
                ->select();
        }
        $orderList = \app\admin\model\Order::where('paytime', 'between time', [$starttime, $endtime])
                ->field('paytime, status, SUM(payamount) AS amount, MIN(paytime) AS min_paytime, MAX(paytime) AS max_paytime, 
            DATE_FORMAT(FROM_UNIXTIME(createtime), "' . $format . '") AS pay_date')
                ->group('pay_date')
                ->select();
        
        

        if ($totalseconds > 84600 * 30 * 2) {
            $starttime = strtotime('last month', $starttime);
            while (($starttime = strtotime('next month', $starttime)) <= $endtime) {
                $column[] = date('Y-m', $starttime);
            }
        } else {
            if ($totalseconds > 86400) {
                for ($time = $starttime; $time <= $endtime;) {
                    $column[] = date("Y-m-d", $time);
                    $time += 86400;
                }
            } else {
                for ($time = $starttime; $time <= $endtime;) {
                    $column[] = date("H:00", $time);
                    $time += 3600;
                }
            }
        }
        $list = array_fill_keys($column, 0);
        if($orderList){
            foreach ($orderList as $k => $v) {
                $list[$v['pay_date']] = round($v['amount'], 2);
            }
        }
        $category = array_keys($list);
        $data = array_values($list);
        return [$category, $data];

    }

    /**
     * 获取发文统计数据
     * @param string $date
     * @return array
     */
    protected function getArchivesStatisticsData($date = '')
    {
        if ($date) {
            list($start, $end) = explode(' - ', $date);

            $starttime = strtotime($start);
            $endtime = strtotime($end);
        } else {
            $starttime = \fast\Date::unixtime('day', 0, 'begin');
            $endtime = \fast\Date::unixtime('day', 0, 'end');
        }
        $totalseconds = $endtime - $starttime;

        $format = '%Y-%m-%d';
        if ($totalseconds > 86400 * 30 * 2) {
            $format = '%Y-%m';
        } else {
            if ($totalseconds > 86400) {
                $format = '%Y-%m-%d';
            } else {
                $format = '%H:00';
            }
        }
        $model_id = $this->request->post("model_id", "");
        $archivesList = \app\admin\model\cms\Archives::with(["admin"])
            ->where('createtime', 'between time', [$starttime, $endtime])
            ->where(function ($query) use ($model_id) {
                if ($model_id) {
                    $query->where('model_id', $model_id);
                }
            })
            ->field('admin_id, createtime, status, COUNT(*) AS nums, MIN(createtime) AS min_createtime, MAX(createtime) AS max_createtime, 
            DATE_FORMAT(FROM_UNIXTIME(createtime), "' . $format . '") AS create_date')
            ->group('admin_id,create_date')
            ->select();

        if ($totalseconds > 84600 * 30 * 2) {
            $starttime = strtotime('last month', $starttime);
            while (($starttime = strtotime('next month', $starttime)) <= $endtime) {
                $column[] = date('Y-m', $starttime);
            }
        } else {
            if ($totalseconds > 86400) {
                for ($time = $starttime; $time <= $endtime;) {
                    $column[] = date("Y-m-d", $time);
                    $time += 86400;
                }
            } else {
                for ($time = $starttime; $time <= $endtime;) {
                    $column[] = date("H:00", $time);
                    $time += 3600;
                }
            }
        }
        $list = [];
        $dataList = [];
        $columnList = array_fill_keys($column, 0);
        foreach ($archivesList as $k => $v) {
            $nickname = $v->admin ? $v->admin->nickname : "未知";
            if (!isset($list[$nickname])) {
                $list[$nickname] = $columnList;
            }
            $list[$nickname][$v['create_date']] = $v['nums'];
        }
        foreach ($list as $index => $item) {
            $dataList[] = [
                'name'      => $index,
                'type'      => 'line',
                'smooth'    => true,
                'areaStyle' => [],
                'data'      => array_values($item)
            ];
        }
        $columnList = array_keys($columnList);
        return [$columnList, $dataList];

    }

}
